package org.study;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.ibatis.annotations.Mapper;

import java.sql.Types;
import java.util.Collections;

/**
 * MyBatis-Plus 的代码生成器(新版本)。
 * 参考 https://baomidou.com/pages/779a6e
 * 参考 https://baomidou.com/pages/981406
 *
 * @author Administrator
 */
public class CodeGenerator {
    private static String projectPath = System.getProperty("user.dir") + "/mybatis-code-generator/mybatis-plus-auto-generator";
    private static String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    private static String username = "root";
    private static String password = "root";

    public static void main(String[] args) {
        FastAutoGenerator.create(jdbcUrl, username, password)
                .globalConfig(builder -> {
                    builder.author("admin")// 设置作者
                            // .enableSwagger()// 开启 swagger 模式
                            .enableSpringdoc()// 开启 springdoc 模式
                            .dateType(DateType.TIME_PACK)// 使用java.time包下的新时间类型
                            .outputDir(projectPath + "/src/main/java");// 指定输出目录
                }).dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                ).packageConfig(builder -> {
                    builder.parent("org.study.generator.auto")// 设置父包名
                            .moduleName("student")// 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper"));// 设置 mapperXml 生成路径
                }).strategyConfig(builder -> {
                    // 策略配置，参考 https://baomidou.com/pages/981406/#entity-策略配置
                    builder.addInclude("t_student")// 设置需要生成的表名
                            .addTablePrefix("t_", "c_")// 设置过滤表前缀
                            // 实体策略配置
                            .entityBuilder()
                            //.superClass(BaseEntity.class)
                            .enableFileOverride()
                            .enableLombok()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            // mapper 策略配置
                            .mapperBuilder()
                            //.superClass(BaseMapper.class)
                            .enableFileOverride()
                            .mapperAnnotation(Mapper.class)
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            // service 策略配置
                            .serviceBuilder()
                            //.superServiceClass(BaseService.class)
                            //.superServiceImplClass(BaseServiceImpl.class)
                            .enableFileOverride()
                            // controller 策略配置
                            .controllerBuilder()
                            //.superClass(BaseController.class)
                            .enableFileOverride()
                            .enableHyphenStyle()
                            .enableRestStyle()
                    ;
                }).templateEngine(
                        // 使用 Freemarker 引擎模板，默认的是 Velocity 引擎模板
                        new FreemarkerTemplateEngine()
                ).execute();
    }
}
